﻿using System.Collections.Generic;
using System;

namespace HREngine.Bots
{
    public partial class Behavior巨化德 : Behavior
    {
        public override string BehaviorName() { return "巨化德"; }

        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }
            // 特殊牌型，贪到下回合出
            int special = 0;
            if(p.ownMaxMana == 1)
            {
                foreach (Handmanager.Handcard hc in p.owncards)
                {
                    if (hc.card.chnName == "聒噪怪") special += 100;
                    if (hc.card.chnName == "幸运币") special += 100;
                    if (hc.card.chnName == "玉莲印记") special += 10;
                }
                if(special >= 310 && (card.chnName == "聒噪怪" || card.chnName == "幸运币" || card.chnName == "玉莲印记") &&(p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.ROGUE || p.enemyHeroStartClass == TAG_CLASS.DEMONHUNTER || p.enemyHeroStartClass == TAG_CLASS.DRUID || p.enemyMinions.Count > 0))
                {
                    return 1000;
                }
            }
            
            // 初始惩罚值
            int pen = 0;
            pen = 0;
            foreach (Minion m in p.enemyMinions)
            {
                if (m.handcard.card.chnName == "末日预言者" && card.type == CardDB.cardtype.MOB && !card.Rush && !card.Charge)
                    pen += 1000; 
            }
            if (p.patchesInDeck && (card.race == 23 || card.race == 26)) pen -= 8; 
            int bonus = 4;
            switch (card.chnName)
            {
                case "树木生长":
                    if (p.ownMinions.Count <= 2) pen += 10;
                    else pen -= 10;
                    break;
                case "远古祝福":
                case "兽群领袖":
                    if (p.ownMinions.Count <= 2) pen += 20;
                    else pen -= 10;
                    pen -= 2;
                    break;
                case "玉莲印记":
                    if (p.ownMinions.Count <= 2) pen += 20;
                    break;
                case "鬼灵爬行者":
                    pen -= bonus * 2;
                    break;
                case "幸运币":
                    pen += 5;
                    break;
                case "聒噪怪":
                    // 对面法师先手爆炸可不敢贪一个聒噪怪
                    if(p.ownMaxMana == 1 && p.enemySecretCount > 0 && p.enemyHeroStartClass == TAG_CLASS.MAGE)
                    {
                        pen += bonus * 2;
                    }
                    if(p.enemyMinions.Count > 0)
                    {
                        pen += bonus * 2;
                    }
                    break;
                case "野蛮咆哮":
                    if(p.ownMinions.Count == 0 && p.enemyHero.Hp > 10)
                    {
                        pen += 100;
                    }
                    if (p.ownMinions.Count <= 2 && p.enemyHero.Hp > 10)
                    {
                        pen += 20;
                    }
                    break;
                case "变形":
                    foreach(Handmanager.Handcard hc in p.owncards)
                    {
                        if (hc.manacost <= 2) pen += bonus * 2;
                    }
                    pen += bonus;
                    break;
            }
            return pen;
        }

        PenalityManager penman = PenalityManager.Instance;
        // 核心，场面值
        public override float getPlayfieldValue(Playfield p)
        {
            if (p.value > -20000)
            {
                return p.value;
            }
            float retval = 0;
            retval += getGeneralVal(p);
            // 危险血线
            int hpboarder = 10;
            // 抢脸血线
            int aggroboarder = 20;
            retval += getHpValue(p, hpboarder, aggroboarder);
            // 对手基本随从交换模拟
            retval += enemyTurnPen(p);
            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;
            
            // 对手空场
            //if (p.enemyMinions.Count == 0) retval += 4;

            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                    case actionEnum.attackWithMinion:
                        if (a.target != null && a.target.handcard.card.chnName == "前沿哨所")
                        {
                            retval -= i * 5;
                        }
                        continue;
                    case actionEnum.useHeroPower:
                        continue;
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                switch (a.card.card.chnName)
                {
                    case "生命分流":
                        retval -= i * 4;
                        if (p.enemyHero.Hp < 5) retval += 10;
                        break;
                    case "森然巨化":
                        if(p.enemySecretCount > 0)
                        {
                            retval -= i * 10;
                        }
                        break;
                }
            }
          
            retval += getSecretPenality(p); // 奥秘的影响
            //p.value = retval;
            return retval;
        }

        // 发现卡的价值
        public override int getDiscoverVal(CardDB.Card card, Playfield p)
        {
            switch (card.chnName)
            {
                case "隐秘的智慧":
                case "救赎":
                case "复仇":
                case "自动防御矩阵":
                case "崇高牺牲":
                    return 5;

            }
            return 0;
        }


        // 敌方随从价值 主要等于 （HP + Angr） * 3 
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            bool dieNextTurn = false;
            foreach (Minion mm in p.enemyMinions)
            {
                if (mm.handcard.card.chnName == "末日预言者")
                {
                    dieNextTurn = true;
                    break;
                }
            }
            foreach (CardDB.cardIDEnum s in p.ownSecretsIDList)
            {
                if (s == CardDB.cardIDEnum.EX1_610 || s == CardDB.cardIDEnum.VAN_EX1_610)
                {
                    if (m.Hp <= 2)
                    {
                        dieNextTurn = true;
                        break;
                    }
                }
            }
            if (m.destroyOnEnemyTurnEnd || m.destroyOnEnemyTurnStart || m.destroyOnOwnTurnEnd || m.destroyOnOwnTurnStart) dieNextTurn = true;
            if (dieNextTurn)
            {
                return -1;
            }
            if (m.Hp <= 0) return 0;
            int bonus = 3;
            int retval = 0;
            if (p.enemyQuest != null && p.enemyQuest.questProgress != 0 && p.enemyHeroStartClass == TAG_CLASS.PALADIN)
            {
                retval += 20;
            }
            if (m.Hp == 1 && m.Angr == 1) retval -= bonus;
            if (m.Angr > 0 || p.enemyHeroStartClass == TAG_CLASS.PALADIN || p.enemyHeroStartClass == TAG_CLASS.PRIEST)
                retval += m.Hp * bonus;
            // 法强相当于 1 点属性
            retval += m.spellpower * bonus;
            if (!m.frozen && !m.cantAttack)
            {
                retval += m.Angr * bonus;
                if(p.ownHero.Hp < 15) retval += m.Angr * bonus / 2;
                if (m.windfury) retval += m.Angr * 2;
            }
            if (m.silenced) return retval;

            if (m.taunt) retval += bonus * 3;
            if (m.divineshild) retval += m.Angr * 2;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 2;

            if (m.handcard.card.race == 14) retval += m.Angr + m.Hp + bonus * 2;

            // 剧毒价值 2 点属性
            if (m.poisonous && m.Angr < 3)
            {
                retval += bonus * 2 ;
            }
            // 吸血很致命
            if (m.lifesteal) retval += m.Angr * 6;
            
            switch (m.handcard.card.chnName){
                case "聒噪怪":
                    if (m.Spellburst) retval += bonus * 20;
                    break;
                case "对空奥术法师":
                case "火焰术士弗洛格尔":
                    retval += bonus * 20;
                    break;
                // 解不掉游戏结束
                case "黑眼":
                case "原野联络人":
                case "灵魂窃贼":
                case "紫罗兰法师":
                case "狂欢报幕员":
                case "巫师学徒":
                case "塔姆辛·罗姆":
                case "导师火心":
                case "伊纳拉·碎雷":
                case "暗影珠宝师汉纳尔":
                case "伦萨克大王":
                case "洛卡拉":
                case "观星者露娜":
                case "火妖":
                case "空中炮艇":
                case "船载火炮":
                case "团伙核心":
                case "巡游领队":
                case "科卡尔驯犬者":
                case "甜水鱼人斥候":
                case "火舌图腾":
                    retval += bonus * 8;
                    break;
                case "末日预言者":
                    foreach(Minion mm in p.ownMinions)
                    {
                        retval += (mm.Hp + mm.Angr) * 5;
                    }
                    break;
                // 不解巨大劣势
                case "大法师瓦格斯":
                case "下水道渔人":
                case "布莱恩·铜须":
                case "法师猎手":
                case "萨特监工":
                case "甩笔侏儒":
                case "精英牛头人酋长，金属之神":
                case "前沿哨所":
                case "莫尔杉哨所":
                case "凯瑞尔·罗姆":
                case "鱼人领军":
                case "南海船长":
                case "坎雷萨德·埃伯洛克":
                case "人偶大师多里安":
                case "暗鳞先知":
                case "灭龙弩炮":
                case "神秘女猎手":
                case "鲨鳍后援":
                case "怪盗图腾":
                case "矮人神射手":
                case "任务达人":
                case "贪婪的书虫":
                case "战马训练师":
                case "相位追猎者":
                case "鱼人宝宝车队":
                case "科多兽骑手":
                case "奥秘守护者":
                case "获救的流民":
                case "低阶侍从":
                    retval += bonus * 2;
                    break;
                // 算有点用
                case "战斗邪犬":
                case "饥饿的秃鹫":
                case "法力浮龙":
                case "加基森拍卖师":
                case "飞刀杂耍者":
                case "锈水海盗":
                case "大法师安东尼达斯":
                case "白银之手新兵":
                    retval += bonus * 1;
                    break;

                case "力量图腾":
                    retval -= 10;
                    break;
            }
            return retval;
        }
        // 我方随从价值 主要等于 （HP + Angr） * 4 + 6
        public override int getMyMinionValue(Minion m, Playfield p)
        {
            bool dieNextTurn = false;
            foreach (Minion mm in p.enemyMinions)
            {
                if (mm.handcard.card.chnName == "末日预言者")
                {
                    dieNextTurn = true;
                    break;
                }
                if (mm.handcard.card.chnName == "火焰术士弗洛格尔" || mm.handcard.card.chnName == "对空奥术法师")
                {
                    if (mm.poisonous)
                        dieNextTurn = true;
                    if (m.Hp <= 2) dieNextTurn = true;
                    break;
                }
            }
            if (m.destroyOnEnemyTurnEnd || m.destroyOnEnemyTurnStart || m.destroyOnOwnTurnEnd || m.destroyOnOwnTurnStart) dieNextTurn = true;
            if (dieNextTurn)
            {
                return -1;
            }
            int retval = 10;
            int bonus = 4;
            if (m.Hp <= 0) return 0;
            retval += (m.Hp + m.Angr) * bonus;
            // 高攻低血是垃圾
            if (m.Angr > m.Hp + 4) retval -= (m.Angr - m.Hp) * (bonus - 1);
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild) retval += m.Angr * 3;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 2 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 2 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;
            // 鱼人种族价值
            if (m.handcard.card.race == 14 || m.handcard.card.race == 26)
            {
                retval += bonus;
            }
            // 剧毒价值 2 点属性
            if (m.poisonous && m.Angr < 3)
            {
                retval += bonus * 2;
            }
            if (m.reborn)
            {
                retval += bonus;
            }
            switch (m.handcard.card.chnName)
            {
                case "猎豹":
                    retval -= bonus * 2;
                    break;
                case "尼鲁巴蛛网领主":
                    retval += bonus ;
                    if(p.enemyHeroStartClass == TAG_CLASS.MAGE || p.enemyHeroStartClass == TAG_CLASS.SHAMAN || p.enemyHeroStartClass == TAG_CLASS.WARLOCK)
                    {
                        retval += bonus * 2;
                    }
                    break;
                case "聒噪怪":
                    break;
                case "贪婪的书虫":
                    if (p.owncards.Count < 3)
                        retval += bonus * (3 - p.owncards.Count);
                    break;
            }
            return retval;
        }

        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }

        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
            //switch (card.name)
            //{
            //    case CardDB.cardName.lesserheal: return 1;
            //    case CardDB.cardName.shapeshift: return 6;
            //    case CardDB.cardName.fireblast:
            //        if (p.enemyHero.Hp <= 1) return 100;
            //        return 7;
            //    case CardDB.cardName.armorup: return 0;
            //    case CardDB.cardName.lifetap: return 9;
            //    case CardDB.cardName.demonclaws:
            //        if (p.enemyHero.Hp <= 1) return 100;
            //        return 10;
            //    case CardDB.cardName.daggermastery:
            //        if (p.enemyHero.Hp <= 1) return 100;
            //        return 5;
            //    case CardDB.cardName.reinforce: return 4;
            //    case CardDB.cardName.totemiccall: return 2;
            //    case CardDB.cardName.steadyshot: return 8;
            //}
            //return -1;
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 1 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.armorup, 0 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.demonclaws, 10 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.totemiccall, 2 },
            { CardDB.cardName.steadyshot, 11 }
        };


        public override int getHpValue(Playfield p, int hpboarder, int aggroboarder)
        {
            int retval = 0;
            // 血线安全
            if (p.ownHero.Hp + p.ownHero.armor > hpboarder)
            {
                retval +=  2 * (5 + p.ownHero.Hp + p.ownHero.armor - hpboarder);
            }
            // 快死了
            else
            {
                if (p.nextTurnWin()) retval -= 2 * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
                else retval -= 5 * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor) * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
            }
            if (p.ownHero.Hp + p.ownHero.armor < 10 && p.ownHero.Hp + p.ownHero.armor > 0)
            {
                retval -= 200 / (p.ownHero.Hp + p.ownHero.armor);
            }
            // 对手血线安全
            if (p.enemyHero.Hp + p.enemyHero.armor >= aggroboarder)
            {
                retval += 3 * (aggroboarder - p.enemyHero.Hp - p.enemyHero.armor);
            }
            // 开始打脸
            else
            {
                retval += 5 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            // 进入斩杀线
            if (p.enemyHero.Hp + p.enemyHero.armor <= 3)
            {
                retval += 50;
            }
            return retval;
        }

    }



}


